package com.school.controller;


import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.school.entity.HljtClass;
import com.school.entity.HljtClassimage;
import com.school.entity.HljtDuty;
import com.school.entity.HljtOndutytest;
import com.school.entity.HljtStudent;
import com.school.entity.android.Education;
import com.school.entity.web.ClassImage;
import com.school.entity.web.DutyTime;
import com.school.entity.web.OnDuty;
import com.school.service.HljtClassService;
import com.school.service.HljtOtherInterfacesService;
import com.school.service.HljtStudentService;
import com.school.service.impl.HljtClassServiceIpml;
import com.school.service.impl.HljtClassimageServiceImpl;
import com.school.service.impl.HljtDutySerivceImpl;
import com.school.service.impl.HljtOndutytestServicesImpl;
import com.school.util.JsonInterface;
import com.school.util.JsonUtil;

import com.school.util.Message;
import com.school.util.RepUtil;
import com.school.util.WhetherNotToUpdate;

import net.sf.json.JSONArray;
import com.alibaba.fastjson.JSON;


@Controller
public class HljtStudentController {
	
	@Autowired
	private HljtStudentService hljtStudentService;
	@Autowired
	private HljtClassServiceIpml hljtClassServiceIpml;
	@Autowired
	private HljtDutySerivceImpl hljtDutySerivceImpl;
//	@Autowired
//	private TestOductyServiceImpl testOductyServiceImpl;
	@Autowired
	private HljtClassimageServiceImpl hljtClassimageServiceImpl;
	@Autowired
	private HljtClassService hljtClassService;
	@Autowired
	private HljtOndutytestServicesImpl hljtOndutytestServicesImpl;
	@Autowired
	HljtOtherInterfacesService hljtOtherInterfacesService;
	
	
	/**
	 * 班学生作品  参数type classid
	 * @param hljtSchoolimage
	 * @param jsonUtil 
	 * @return
	 */
	@RequestMapping("/studentWorks")
	@ResponseBody
	public JSONObject studentWorks(HljtStudent hljtStudent , JsonUtil jsonUtil) {
		try {
			if(null !=hljtStudent.getClassid() && hljtStudent.getType().equals("5")) {
				List<HljtStudent>	temp = hljtStudentService.studentWorks(hljtStudent);
				if(null != temp && temp.size()>0) {
					for (HljtStudent hljt : temp) {
						hljt.setImagePath(WhetherNotToUpdate.getIp()+hljt.getImagePath());
					}
		  		//	jsonUtil.setSucceed(jsonUtil.setSucceedMsg("该班学生作品", temp), "操作成功");
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"该班学生作品");

				}else {
					jsonUtil.setSchool("该班学生作品空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}
	
	
	
	/**
	 * 班学生荣誉  参数type classid
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/studentHonor")
	@ResponseBody
	public JSONObject studentHonor (HljtStudent hljtStudent , JsonUtil jsonUtil) {
		try {
			if(null !=hljtStudent.getClassid() && hljtStudent.getType().equals("1")) {
				List<HljtStudent>	temp = hljtStudentService.studentWorks(hljtStudent);
				if(null != temp && temp.size()>0) {
					for (HljtStudent hljt : temp) {
						hljt.setImagePath(WhetherNotToUpdate.getIp()+hljt.getImagePath());
					}
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"该班学生荣誉");

				}else {
					jsonUtil.setSchool("该班学生荣誉空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}

	

	
	/**
	 * 德育值日
	 * @param classid
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/toBeOnDuty")
	@ResponseBody
	public JsonInterface toBeOnDuty(Integer classid) throws Exception{
		JsonInterface jsonInterface=new JsonInterface();
		String newIp= WhetherNotToUpdate.getIp();
		
	//	List<HljtOnduty> HljtOnduty=new ArrayList<>();
		
		if(null !=classid) {
			//获取所有德育模式
			List<HljtDuty> hljtDuty=hljtDutySerivceImpl.findAll();
			if(null!=hljtDuty&&hljtDuty.size()>0) {
				System.out.println(hljtDuty.size());
				List<Education> educationList=new ArrayList<>();
				
				for(int i=0;i<hljtDuty.size();i++) {
					Education education=new Education();
					HljtOndutytest HljtOndutytest=hljtOndutytestServicesImpl.findOnduty(classid, hljtDuty.get(i).getId());
					System.out.println("a");
					if(null!=HljtOndutytest) {
						//设置德育模式图片
						education.setImageip(newIp+hljtDuty.get(i).getDutyimage());
						//设置德育模式名字
						education.setName(hljtDuty.get(i).getDutyname());
						//
						education.setMondayStudent(HljtOndutytest.getMonday());
						//
						education.setTuesdayStudent(HljtOndutytest.getTuesday());
						//
						education.setWednesdayStudent(HljtOndutytest.getWednesday());
						//
						education.setThursdayStudent(HljtOndutytest.getThursday());
						//
						education.setFridayStudent(HljtOndutytest.getFriday());
						//
						education.setSaturdayStudent(HljtOndutytest.getSaturday());
						//
						education.setSundayStudent(HljtOndutytest.getSunday());
						
						educationList.add(education);
						JSONArray array=JSONArray.fromObject(educationList);
						jsonInterface.setData(array.toString());
						jsonInterface.setErrCode(0);
						jsonInterface.setErrMsg("请求成功");
					}else {
						jsonInterface.setErrCode(1);
						jsonInterface.setErrMsg("无数据，请添加数据！");
					}
				}
			}else {
				jsonInterface.setErrCode(1);
				jsonInterface.setErrMsg("无数据，请添加数据！");
			}
			
		}else {
			jsonInterface.setErrCode(5);
			jsonInterface.setErrMsg("请求失败");
		}
		return jsonInterface;
	}

	/**
	 * 添加学生多个
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/addStudent")
	@ResponseBody
	public JSONObject addStudent (String data ,HttpServletResponse rep ,HljtStudent hljtStudent , JsonUtil jsonUtil) {
		JSONObject jSONObject = new JSONObject();
		RepUtil.repPake(rep);
		System.out.println(data);
		try {
			if(null !=data && ""!=data) {
				JSONObject js = JSON.parseObject(data);
				Integer classid = js.getInteger("classid");
				String stuInfos = js.getString("stuInfos");
				Integer typeOr = js.getInteger("typeOr");
				if(null !=classid  &&null !=stuInfos && ""!=stuInfos) {
					List<HljtStudent> hljtDeviceManagement = JSON.parseArray(stuInfos, HljtStudent.class);
					if(null !=hljtDeviceManagement && hljtDeviceManagement.size()>0) {
						for (HljtStudent temp : hljtDeviceManagement) {
							temp.setClassid(classid);
							temp.setTypeOr(typeOr);
						}
						Integer	temp = hljtStudentService.addStudent(hljtDeviceManagement);
						if(hljtDeviceManagement.size()==temp) {
							jSONObject.put("success", true);
						}else {
							jSONObject.put("success", false);
						}
					}else {
						jSONObject.put("parameterError","参数错误");
						jSONObject.put("parameter",data);
					}
					
				}else {
					jSONObject.put("parameterError","参数错误");
					jSONObject.put("parameter",data);
				}
			}else {
				jSONObject.put("parameterError","参数错误");
				jSONObject.put("parameter",data);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jSONObject.put("serverException", "服务器异常");
		}
		return jSONObject;
		
	}
	
	//=====================================================================================================================================================
	
	/**
	 * 添加学生单个
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/addStudentSingle")
	@ResponseBody
	public JSONObject addStudentSingle(String data ,HttpServletResponse rep ,HljtStudent hljtStudent , JsonUtil jsonUtil) {
		JSONObject jSONObject = new JSONObject();
		RepUtil.repPake(rep);
		System.out.println(data);
		try {
			if(null !=data && ""!=data) {
				List<HljtStudent> hljtDeviceManagement = JSON.parseArray(data, HljtStudent.class);
				if(null !=hljtDeviceManagement && hljtDeviceManagement.size()>0) {
					Integer	temp = hljtStudentService.addStudent(hljtDeviceManagement);
					if(hljtDeviceManagement.size()==temp) {
						jSONObject.put("success", true);
					}else {
						jSONObject.put("success", false);
					}
				}else {
					jSONObject.put("parameterError","参数错误");
					jSONObject.put("parameter",data);
				}
			}else {
				jSONObject.put("parameterError","参数错误");
				jSONObject.put("parameter",data);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jSONObject.put("serverException", "服务器异常");
		}
		return jSONObject;
		
	}
	
	/**
	 * 修改学生
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/updateStudent")
	@ResponseBody
	public JSONObject updateStudent (String data ,HttpServletResponse rep ,HljtStudent hljtStudent , JsonUtil jsonUtil) {
		JSONObject jSONObject = new JSONObject();
		RepUtil.repPake(rep);
		try {
			if(null !=data && ""!=data) {
				HljtStudent hljtDeviceManagement = JSON.parseObject(data, HljtStudent.class);
				Integer	temp = hljtStudentService.updateStudentInfoByClassId(hljtDeviceManagement);
				if(temp>0) {
					jSONObject.put("success", true);
				}else {
					jSONObject.put("success", false);
				}
			}else {
				jSONObject.put("parameterError","参数错误");
				jSONObject.put("parameter",data);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jSONObject.put("serverException", "服务器异常");
		}
		return jSONObject;
		
	}
	
	
	
	/**
	 * 删除学生
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/deleteStudent")
	@ResponseBody
	public JSONObject deleteStudent(String data ,HttpServletResponse rep ,HljtStudent hljtStudent , JsonUtil jsonUtil) {
		JSONObject jSONObject = new JSONObject();
		RepUtil.repPake(rep);
		System.out.println(data);
		try {
			if(null !=data && ""!=data) {
				List<HljtStudent> hljtDeviceManagement = JSON.parseArray(data, HljtStudent.class);
				Integer	temp = hljtStudentService.updateStudentByClassId(hljtDeviceManagement);
				if(hljtDeviceManagement.size()==temp) {
					jSONObject.put("success", true);
				}else {
					jSONObject.put("success", false);
				}
			}else {
				jSONObject.put("parameterError","参数错误");
				jSONObject.put("parameter",data);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jSONObject.put("serverException", "服务器异常");
		}
		return jSONObject;
		
	}
	
	
	/**
	 * 学生管理
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/studentManagement")
	@ResponseBody
	public JSONObject studentManagement(HljtStudent hljtStudent , JsonUtil jsonUtil , HttpServletResponse rep) {
		JSONObject jSONObject = new JSONObject();
		try {
			RepUtil.repPake(rep);
			
			if(null !=hljtStudent && null != hljtStudent.getPageIndex()&&
					null!=hljtStudent.getPageSize()&&null!=hljtStudent.getClassid()) {
				
				hljtStudent.setPageNum((hljtStudent.getPageIndex()-1)*hljtStudent.getPageSize());
				jSONObject.put("studentIdList", hljtStudentService.selectStudentByClassId(hljtStudent));
			}else {
				jSONObject.put("parameterError","参数错误");
				jSONObject.put("parameter",hljtStudent);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jSONObject.put("serverException", "服务器异常");
		}
		return jSONObject;
		
	}
	


	 /**
	  * 查找学生作品
	  * @param classid
	  * @param pageSize
	  * @param pageIndex
	  * @param rep
	  * @return
	 * @throws Exception 
	  */
 	@RequestMapping("/selectstudentworks")
	@ResponseBody
	public Message selectStudentWorks(Integer classid,String type,String studentname,Integer pageSize,Integer pageIndex,HttpServletResponse rep) throws Exception{
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		String newIp= WhetherNotToUpdate.getIp();
		if(null !=classid) {
			List<ClassImage> hljtClassImages=new ArrayList<>();
			if(classid>-1) {
				
				if(null !=studentname) {
					pageIndex=(pageIndex-1)*pageSize;
					List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(classid, null, null,null);
					List<HljtClassimage> hljtclassimageall=hljtClassimageServiceImpl.findListImagepage(null, null, classid, type,studentname);
					System.out.println(hljtclassimageall.size());
					List<HljtClassimage> hljtclassimage=hljtClassimageServiceImpl.findListImagepage(pageSize, pageIndex, classid, type,studentname);
					for(int i=0;i<hljtclassimage.size();i++) {
						ClassImage HljtClassImage=new ClassImage();
						HljtClassImage.setId(hljtclassimage.get(i).getId());
						HljtClassImage.setClassid(hljtclassimage.get(i).getClassid());
						HljtClassImage.setImagepath(newIp+hljtclassimage.get(i).getImagepath());
						HljtClassImage.setTitle(hljtclassimage.get(i).getTitle());
						HljtClassImage.setContent(hljtclassimage.get(i).getContent());
						HljtClassImage.setStudentname(hljtclassimage.get(i).getStudentname());
						
						HljtClass HljtClass=hljtClassService.findClass(hljtclassimage.get(i).getClassid());
						HljtClassImage.setClasses(HljtClass.getGrade());
						HljtClassImage.setLevel(HljtClass.getClasses());
						
						hljtClassImages.add(HljtClassImage);
					}
					data.put("data", hljtClassImages);
					data.put("Total", hljtclassimageall.size());
					data.put("class", HljtClasses);
					data.put("success", true);
				}else {
					pageIndex=(pageIndex-1)*pageSize;
					List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(classid, null, null,null);
					List<HljtClassimage> hljtclassimageall=hljtClassimageServiceImpl.findListImagepage(null, null, classid, type,null);
					System.out.println(hljtclassimageall.size());
					List<HljtClassimage> hljtclassimage=hljtClassimageServiceImpl.findListImagepage(pageSize, pageIndex, classid, type,null);
					for(int i=0;i<hljtclassimage.size();i++) {
						ClassImage HljtClassImage=new ClassImage();
						HljtClassImage.setId(hljtclassimage.get(i).getId());
						HljtClassImage.setClassid(hljtclassimage.get(i).getClassid());
						HljtClassImage.setImagepath(newIp+hljtclassimage.get(i).getImagepath());
						HljtClassImage.setTitle(hljtclassimage.get(i).getTitle());
						HljtClassImage.setContent(hljtclassimage.get(i).getContent());
						HljtClassImage.setStudentname(hljtclassimage.get(i).getStudentname());
						
						HljtClass HljtClass=hljtClassService.findClass(hljtclassimage.get(i).getClassid());
						HljtClassImage.setClasses(HljtClass.getGrade());
						HljtClassImage.setLevel(HljtClass.getClasses());
						
						hljtClassImages.add(HljtClassImage);
					}
					data.put("data", hljtClassImages);
					data.put("Total", hljtclassimageall.size());
					data.put("class", HljtClasses);
					data.put("success", true);
				}
				
				
				
			}else {
				if(null !=studentname) {
					pageIndex=(pageIndex-1)*pageSize;
					classid=null;
					List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(classid, null, null,null);
					List<HljtClassimage> hljtclassimageall=hljtClassimageServiceImpl.findListImagepage(null, null, classid, type,studentname);
					List<HljtClassimage> hljtclassimage=hljtClassimageServiceImpl.findListImagepage(pageSize, pageIndex, classid, type,studentname);
					for(int i=0;i<hljtclassimage.size();i++) {
						ClassImage HljtClassImage=new ClassImage();
						HljtClassImage.setId(hljtclassimage.get(i).getId());
						HljtClassImage.setClassid(hljtclassimage.get(i).getClassid());
						HljtClassImage.setImagepath(newIp+hljtclassimage.get(i).getImagepath());
						HljtClassImage.setTitle(hljtclassimage.get(i).getTitle());
						HljtClassImage.setContent(hljtclassimage.get(i).getContent());
						HljtClassImage.setStudentname(hljtclassimage.get(i).getStudentname());
						
						HljtClass HljtClass=hljtClassService.findClass(hljtclassimage.get(i).getClassid());
						HljtClassImage.setClasses(HljtClass.getGrade());
						HljtClassImage.setLevel(HljtClass.getClasses());
						
						hljtClassImages.add(HljtClassImage);
					}
					data.put("data", hljtClassImages);
					data.put("Total", hljtclassimageall.size());
					data.put("class", HljtClasses);
					data.put("success", true);
				}else {
					pageIndex=(pageIndex-1)*pageSize;
					classid=null;
					List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(classid, null, null,null);
					List<HljtClassimage> hljtclassimageall=hljtClassimageServiceImpl.findListImagepage(null, null, classid, type,null);
					List<HljtClassimage> hljtclassimage=hljtClassimageServiceImpl.findListImagepage(pageSize, pageIndex, classid, type,null);
					for(int i=0;i<hljtclassimage.size();i++) {
						ClassImage HljtClassImage=new ClassImage();
						HljtClassImage.setId(hljtclassimage.get(i).getId());
						HljtClassImage.setClassid(hljtclassimage.get(i).getClassid());
						HljtClassImage.setImagepath(newIp+hljtclassimage.get(i).getImagepath());
						HljtClassImage.setTitle(hljtclassimage.get(i).getTitle());
						HljtClassImage.setContent(hljtclassimage.get(i).getContent());
						HljtClassImage.setStudentname(hljtclassimage.get(i).getStudentname());
						
						HljtClass HljtClass=hljtClassService.findClass(hljtclassimage.get(i).getClassid());
						HljtClassImage.setClasses(HljtClass.getGrade());
						HljtClassImage.setLevel(HljtClass.getClasses());
						
						hljtClassImages.add(HljtClassImage);
					}
					data.put("data", hljtClassImages);
					data.put("Total", hljtclassimageall.size());
					data.put("class", HljtClasses);
					data.put("success", true);
				}
				
				
			}
		}else {
			data.put("success",  false);
		}
		Message.setData(data);
		return Message;
 }
	 /**
	  * 删除学生作品
	  * @param id
	  * @param rep
	  * @return
	  */
 	@RequestMapping("/deletestudentworks")
	@ResponseBody
	public Message deleteStudentWorks( Integer classid ,Integer id,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null !=id) {
			hljtClassimageServiceImpl.deleteclassImage(id);
			hljtOtherInterfacesService.modifyByCalssId(classid);
			data.put("success", true);
		}else {
			
			data.put("success",  false);
		}
		Message.setData(data);
		return Message;
 	}
	/**
	 * 添加学生作品
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/addstudentworks")
	@ResponseBody
	public Message addStudentWorks(Integer classid,String title,String content,String type,MultipartFile imgFile,String studentname,HttpServletResponse rep) throws Exception {
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		if(null!=classid && null !=title && null!=content && null !=imgFile) {
		if(classid>-1) {
			//生成新的图片名字
			String name = UUID.randomUUID().toString().replaceAll("-", "");
			//图片后缀名
			String ext = FilenameUtils.getExtension(imgFile.getOriginalFilename());
			//保存路径D:\java\apache-tomcat-8.0.48\webapps\image\class
			imgFile.transferTo(new File(WhetherNotToUpdate.getSaveDisk() + name + "." + ext));
			
			//新的图片名字
			String image=name+"."+ext;
			HljtClassimage classimage=new HljtClassimage();
			classimage.setImagepath(WhetherNotToUpdate.getReadDisk()+image);
			classimage.setClassid(classid);
			classimage.setTitle(title);
			classimage.setContent(content);
			classimage.setType(type);
			classimage.setStudentname(studentname);
			
			hljtClassimageServiceImpl.addcassImage(classimage);
			hljtOtherInterfacesService.modifyByCalssId(classid);
			data.put("success", true);
		}else {
			//生成新的图片名字
			String name = UUID.randomUUID().toString().replaceAll("-", "");
			//图片后缀名
			String ext = FilenameUtils.getExtension(imgFile.getOriginalFilename());
			//保存路径D:\java\apache-tomcat-8.0.48\webapps\image\class
			imgFile.transferTo(new File(WhetherNotToUpdate.getSaveDisk() + name + "." + ext));
			//新的图片名字
			String image=name+"."+ext;
			HljtClassimage classimage=new HljtClassimage();
			classimage.setImagepath(WhetherNotToUpdate.getReadDisk()+image);
			classimage.setClassid(classid);
			classimage.setTitle(title);
			classimage.setContent(content);
			classimage.setType(type);
			hljtClassimageServiceImpl.addcassImage(classimage);
			hljtOtherInterfacesService.modifyByCalssId(classid);
			data.put("success", true);
		}
		}else {	
		data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	
	
	/**
	 *修改学生作品
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/updatestudentworks")
	@ResponseBody
	public Message updatestudentworks(Integer classid ,Integer id,String content,String title,MultipartFile imgFile,String studentname,HttpServletResponse rep) throws Exception{
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		if(null !=id && null !=content && null!=title) {
			HljtClassimage classimage=hljtClassimageServiceImpl.findImage(id);
			HljtClassimage hljtClassimage=new HljtClassimage();
			hljtClassimage.setId(id);
			hljtClassimage.setTitle(title);
			hljtClassimage.setContent(content);
			hljtClassimage.setStudentname(studentname);
			if(imgFile.getSize()>0) {
				//生成新的图片名字
				String name = UUID.randomUUID().toString().replaceAll("-", "");
				//图片后缀名
				String ext = FilenameUtils.getExtension(imgFile.getOriginalFilename());
				//保存路径
				imgFile.transferTo(new File(WhetherNotToUpdate.getSaveDisk() + name + "." + ext));
				//新的图片名字
				String image=name+"."+ext;
				hljtClassimage.setImagepath(WhetherNotToUpdate.getReadDisk()+image);
			}else {
				hljtClassimage.setImagepath(classimage.getImagepath());
			}
			hljtClassimageServiceImpl.updateImage(hljtClassimage);
			hljtOtherInterfacesService.modifyByCalssId(classid);
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	/**
	 * 查找班级值日信息
	 * @param classid
	 * @param rep
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectonduty")
	@ResponseBody
	public Message selectOnDuty(Integer classid,HttpServletResponse rep) throws Exception{
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		String newIp= WhetherNotToUpdate.getIp();
		if(null!=classid) {
			if(classid>-1) {
				List<HljtDuty> hljtduty=hljtDutySerivceImpl.findAll();
				List<OnDuty> ondutyall=new ArrayList<>();
				if(null!=hljtduty&&hljtduty.size()>0) {
					for (int i = 0; i < hljtduty.size(); i++) {
						System.out.println("classid:"+classid);
						System.out.println("dutyid:"+hljtduty.get(i).getId());
						
						HljtOndutytest hljtOndutytest=hljtOndutytestServicesImpl.findOnduty(classid, hljtduty.get(i).getId());
						System.out.println(hljtOndutytest==null);
						
						if(null !=hljtOndutytest) {
							OnDuty onduty=new OnDuty();
							onduty.setClassid(classid);
							onduty.setDutyid(hljtduty.get(i).getId());
							onduty.setDutyname(hljtduty.get(i).getDutyname());
							onduty.setDutyimage(newIp+hljtduty.get(i).getDutyimage());
							
							onduty.setFriday(hljtOndutytest.getFriday());
							onduty.setMonday(hljtOndutytest.getMonday());
							onduty.setThursday(hljtOndutytest.getThursday());
							onduty.setTuesday(hljtOndutytest.getTuesday());
							onduty.setWednesday(hljtOndutytest.getWednesday());
							if(null!=hljtOndutytest.getSaturday()) {
								onduty.setSaturday(hljtOndutytest.getSaturday());
							}else {
								onduty.setSaturday("");
							}
							if(null !=hljtOndutytest.getSunday()) {
								onduty.setSunday(hljtOndutytest.getSunday());
							}else {
								onduty.setSunday("");
							}
							ondutyall.add(onduty);
						}else {
							 continue;
						}
					}
					List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(classid, null, null,null);
					data.put("hljtduty", hljtduty);
					data.put("class", HljtClasses);
					data.put("data", ondutyall);
					data.put("success", true);
				}else {
					System.out.println("a");
					List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(classid, null, null,null);
					List<HljtDuty> HljtDuty=new ArrayList<>();
					data.put("hljtduty", HljtDuty);
					data.put("class", HljtClasses);
					data.put("data", ondutyall);
					data.put("success", true);
				}
			}else {
				
				List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(null, null, null,null);
				data.put("class", HljtClasses);
				data.put("success", true);
			}
		}else{
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	/**
	 * 修改值日人
	 * @param classid
	 * @param dutyid
	 * @param rep
	 * @return
	 */
	@RequestMapping("/updateonduty")
	@ResponseBody
	public Message updateOnDuty(DutyTime dutytime,HttpServletResponse rep){
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		if(null !=dutytime) {
			HljtOndutytest HljtOndutytest=new HljtOndutytest();
			HljtOndutytest.setClassid(dutytime.getClassid());
			HljtOndutytest.setDutyid(dutytime.getDutyid());
			HljtOndutytest.setFriday(dutytime.getFriday());
			HljtOndutytest.setMonday(dutytime.getMonday());
			HljtOndutytest.setSaturday(dutytime.getSaturday());
			HljtOndutytest.setSunday(dutytime.getSunday());
			HljtOndutytest.setThursday(dutytime.getThursday());
			HljtOndutytest.setTuesday(dutytime.getTuesday());
			HljtOndutytest.setWednesday(dutytime.getWednesday());
			hljtOndutytestServicesImpl.updateOndyuty(HljtOndutytest);
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	/**
	 * 添加值日人
	 * @param classid
	 * @param dutyid
	 * @param rep
	 * @return
	 */
	@RequestMapping("/addonduty")
	@ResponseBody
	public Message addOnDuty(DutyTime dutytime,HttpServletResponse rep){
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		if(null !=dutytime) {
			HljtOndutytest hljtOndutytest=hljtOndutytestServicesImpl.findOnduty(dutytime.getClassid(), dutytime.getDutyid());
			if(null !=hljtOndutytest) {
//				HljtOndutytest HljtOndutytest=new HljtOndutytest();
//				HljtOndutytest.setClassid(dutytime.getClassid());
//				HljtOndutytest.setDutyid(dutytime.getDutyid());
//				HljtOndutytest.setFriday(dutytime.getFriday());
//				HljtOndutytest.setMonday(dutytime.getMonday());
//				HljtOndutytest.setSaturday(dutytime.getSaturday());
//				HljtOndutytest.setSunday(dutytime.getSunday());
//				HljtOndutytest.setThursday(dutytime.getThursday());
//				HljtOndutytest.setTuesday(dutytime.getTuesday());
//				HljtOndutytest.setWednesday(dutytime.getWednesday());
//				hljtOndutytestServicesImpl.updateOndyuty(HljtOndutytest);
				
				data.put("success", false);
			}else {
			System.out.println(dutytime.getClassid());
			System.out.println(dutytime.getDutyid());
			System.out.println(dutytime.getFriday());
			System.out.println(dutytime.getMonday());
			System.out.println(dutytime.getSaturday());
			System.out.println(dutytime.getSunday());
			System.out.println(dutytime.getThursday());
			System.out.println(dutytime.getTuesday());
			System.out.println(dutytime.getWednesday());
			
			
				HljtOndutytest HljtOndutytest=new HljtOndutytest();
				HljtOndutytest.setClassid(dutytime.getClassid());
				HljtOndutytest.setDutyid(dutytime.getDutyid());
				
				HljtOndutytest.setFriday(dutytime.getFriday());
				HljtOndutytest.setMonday(dutytime.getMonday());
				HljtOndutytest.setSaturday(dutytime.getSaturday());
				HljtOndutytest.setSunday(dutytime.getSunday());
				HljtOndutytest.setThursday(dutytime.getThursday());
				HljtOndutytest.setTuesday(dutytime.getTuesday());
				HljtOndutytest.setWednesday(dutytime.getWednesday());
				
				hljtOndutytestServicesImpl.addOndyuty(HljtOndutytest);
				
				data.put("success", true);
			}
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	
	/**
	 * 删除班级值日学生
	 * @param classid
	 * @param rep
	 * @return
	 */
	@RequestMapping("/deleteonduty")
	@ResponseBody
	public Message deleteOnDuty(Integer classid,Integer dutyid,HttpServletResponse rep){
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		if(null !=classid && null !=dutyid) {
			hljtOndutytestServicesImpl.deleteOnduty(classid, dutyid);
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	
	 public String getIp() throws Exception {
	        String ipString = "";
	        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
	        InetAddress ip = null;
	        while (allNetInterfaces.hasMoreElements()) {
	            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
	            Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
	            while (addresses.hasMoreElements()) {
	                ip = (InetAddress) addresses.nextElement();
	                if (ip != null && ip instanceof Inet4Address && !ip.getHostAddress().equals("127.0.0.1")) {
	                    return ip.getHostAddress();
	                }
	            }
	        }
	        return ipString;
	    }
}
